home *** CD-ROM | disk | FTP | other *** search
/ 100 Plus Great Games 2 / 100PLUSV2.BIN / games / WakeRace.dxr / Internal_4_Collision Functions.ls < prev    next >
Encoding:
Text File  |  2002-01-25  |  2.3 KB  |  92 lines

  1. global gPlayer, gpCollisionSprites
  2.  
  3. on p_Init
  4.   gpCollisionSprites = []
  5. end
  6.  
  7. on p_RegisterObject theObject
  8.   add(gpCollisionSprites, theObject)
  9.   return count(gpCollisionSprites)
  10. end
  11.  
  12. on p_UpdateObject theSlot, newloc, newVel
  13.   gpCollisionSprites[theSlot][1] = newloc
  14.   gpCollisionSprites[theSlot][2] = newVel
  15. end
  16.  
  17. on p_Update
  18.   repeat with i = 1 to count(gpCollisionSprites) - 1
  19.     repeat with j = i + 1 to count(gpCollisionSprites)
  20.       p_checkForCollision(i, j)
  21.     end repeat
  22.   end repeat
  23. end
  24.  
  25. on p_GetDistance loc1, loc2
  26.   if voidp(loc1) or voidp(loc2) then
  27.     return 0
  28.   end if
  29.   diff = loc2 - loc1
  30.   if diff = point(0, 0) then
  31.     return 0
  32.   else
  33.     return sqrt(power(diff[1], 2) + power(diff[2], 2))
  34.   end if
  35. end
  36.  
  37. on p_DotProduct vec1, vec2
  38.   return (vec1[1] * vec2[1]) + (vec1[2] * vec2[2])
  39. end
  40.  
  41. on p_checkForCollision sprite1, sprite2
  42.   loc1 = gpCollisionSprites[sprite1][1]
  43.   rad1 = gpCollisionSprites[sprite1][3]
  44.   loc2 = gpCollisionSprites[sprite2][1]
  45.   rad2 = gpCollisionSprites[sprite2][3]
  46.   dist = p_GetDistance(loc1, loc2)
  47.   if dist = 0 then
  48.     return 
  49.   end if
  50.   if dist <= (rad1 + rad2) then
  51.     p_handleCollision(sprite1, sprite2)
  52.   end if
  53. end
  54.  
  55. on p_handleCollision sprite1, sprite2
  56.   m1 = gpCollisionSprites[sprite1][4]
  57.   v1i = gpCollisionSprites[sprite1][2]
  58.   loc1 = gpCollisionSprites[sprite1][1]
  59.   m2 = gpCollisionSprites[sprite2][4]
  60.   v2i = gpCollisionSprites[sprite2][2]
  61.   loc2 = gpCollisionSprites[sprite2][1]
  62.   loi = loc2 - loc1
  63.   loiLength = p_GetDistance(point(0, 0), loi)
  64.   n = loi / loiLength
  65.   t = point(-n[2], n[1])
  66.   v1in = p_DotProduct(v1i, n)
  67.   v1it = p_DotProduct(v1i, t)
  68.   v2in = p_DotProduct(v2i, n)
  69.   v2it = p_DotProduct(v2i, t)
  70.   v1fn = ((2 * m2 * v2in) + (v1in * (m1 - m2))) / (m1 + m2)
  71.   v2fn = ((2 * m1 * v1in) + (v2in * (m2 - m1))) / (m1 + m2)
  72.   x1f = (n[1] * v1fn) + (t[1] * v1it)
  73.   y1f = (n[2] * v1fn) + (t[2] * v1it)
  74.   v1f = point(x1f, y1f)
  75.   x2f = (n[1] * v2fn) + (t[1] * v2it)
  76.   y2f = (n[2] * v2fn) + (t[2] * v2it)
  77.   v2f = point(x2f, y2f)
  78.   if gpCollisionSprites[sprite1][5] = gPlayer.pMySpriteNum then
  79.     gPlayer.crash(v1f)
  80.   else
  81.     e_Crash(gpCollisionSprites[sprite1][5], v1f)
  82.   end if
  83.   if gpCollisionSprites[sprite2][5] = gPlayer.pMySpriteNum then
  84.     gPlayer.crash(v2f)
  85.   else
  86.     e_Crash(gpCollisionSprites[sprite2][5], v2f)
  87.   end if
  88.   if sound(2).isBusy() = 0 then
  89.     puppetSound(2, "Crash")
  90.   end if
  91. end
  92.